package com.jhy.study2.IO;


import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author: robert
 * @description: 压缩单个文件练习
 * @date: 2021/8/23 18:59
 * @return
 */
public class SingleFileZip1 {
    public static void main(String[] args) {
        zipSingleFile();

    }

    public static void zipSingleFile(){
        File file = new File("D://folder1//folder2//folder3//abc.txt");

        FileInputStream input = null;
        ZipOutputStream zipOutputStream = null;
        try {
            //一、将要压缩的文件读到流中
            input = new FileInputStream(file);
            zipOutputStream = new ZipOutputStream(new FileOutputStream(new File("D://folder1//abc.zip")));

            int r = 0;
            int num = 0;

            //定义压缩文件的名字和注释
            zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
            zipOutputStream.setComment("commnet");
            //二、将读到的数据放到zipEntry中
            //三、将zipEntry放到输出流中
            while(( r = input.read()) != -1){
                System.out.println(r);
                zipOutputStream.write(r);
            }




        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                input.close();
                zipOutputStream.close();
                System.out.println("单个文件压缩完成");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }





    }


}
